<?php
define('IN_WONDERSHARE', true);
//include("../inc/global.php");
require('../includes/init.php');
require('./classes/control/control_store.php');

class Store
{
	function __construct(){
		$this->ControlStore = new ControlStore();
	}

	function index()
	{
		$GLOBALS['smarty']->display('store_index.htm');
	}

	/**************************************
	* 查询所有未删除的Store信息
	* @access  public
	*
	* @return  array
	**************************************/
	function StoreList()
	{
		$storelist=$this->ControlStore->StoreList();
		$GLOBALS['smarty']->assign('storelist',$storelist);
		$GLOBALS['smarty']->display('store_index.htm');
	}
	
   /**************************************
	* 添加新的Store信息
	* @access  public
	*
	* @return  array
	**************************************/
	function AddNewStore()
	{
		$actionurl="./store.php?action=SaveStore";
		$GLOBALS['smarty']->assign('actionurl',$actionurl);
		$GLOBALS['smarty']->display('store_detail.htm');
	}
	
   /**************************************
	* 查询所有未删除的产品信息
	* @access  public
	*
	* @return  array
	**************************************/
	function SProductlist()
	{
		$store_id=intval($_REQUEST['store_id']);
		$productIDlist=$this->ControlStore->SelectStore($store_id);
		$productIDlist=$productIDlist?$productIDlist:0;
		$catname=$this->ControlStore->SelectStorecatname($store_id);
		
		$page = 1;
		      if (!empty($_REQUEST['page'])) {
			  $page = intval($_REQUEST['page']);
		      }
		$url_info=$this->ControlStore->getProductListPage($page);
		$rows=$this->ControlStore->getProductList($url_info['page_info']['start'],12);
		if($rows)
			{
				foreach ($rows as $key => $value)
				{
					if($value['product_state']==0)
					{
						$rows[$key]['product_state']='未发布';
					}
					else 
					{
						$rows[$key]['product_state']='发布';
					}
					if($value['product_is_recommend']==0)
					{
						$rows[$key]['product_is_recommend']='未推荐';
					}
					else 
					{
						$rows[$key]['product_is_recommend']='推荐';
					}
					$rows[$key]['product_add_time']=date('Y-m-d',$value['product_add_time']);
				}
			}

		$GLOBALS['smarty']->assign('page_url',   $url_info['page_url']);
		$GLOBALS['smarty']->assign('total',   $url_info['page_info']['total']);
		$GLOBALS['smarty']->assign('store_id',$store_id);
		$GLOBALS['smarty']->assign('productIDlist',$productIDlist);
		$GLOBALS['smarty']->assign('productlist',$rows);
		$GLOBALS['smarty']->assign('catname',$catname);
		$GLOBALS['smarty']->display('store_link_product.htm');
	}
	
	
		/**************************************
	     * 搜索查询符合条件的未删除的公共产品信息并将数据传入和调用smarty模版
		 * @access  public
		 * @param  
		 *
		 * @return  array
	     **************************************/
		
	
		function SearchProductList()
		{
			$page = 1;
		      if (!empty($_REQUEST['page'])) {
			  $page = intval($_REQUEST['page']);
		      }
		    $store_id=intval($_REQUEST['store_id']); 
		    $product_is_link=intval($_REQUEST['product_is_link']);
		    $search_type=intval($_REQUEST['search_type']);
		    $search_key=trim($_REQUEST['search_key']); 
	
		    $productIDlist=$this->ControlStore->SelectStore($store_id);
			$productIDlist=$productIDlist?$productIDlist:0;
		    $url_info=$this->ControlStore->SearchProductListPage($page,$product_is_link,$search_type,$search_key,$store_id);
			$rows = $this->ControlStore->SearchProductList($url_info['page_info']['start'],12,$product_is_link,$search_type,$search_key,$store_id);
			if($rows)
			{
				foreach ($rows as $key => $value)
				{
					if($value['product_state']==0)
					{
						$rows[$key]['product_state']='未发布';
					}
					else 
					{
						$rows[$key]['product_state']='发布';
					}
					if($value['product_is_recommend']==0)
					{
						$rows[$key]['product_is_recommend']='未推荐';
					}
					else 
					{
						$rows[$key]['product_is_recommend']='推荐';
					}
					$rows[$key]['product_add_time']=date('Y-m-d',$value['product_add_time']);
				}
			}
			
			$GLOBALS['smarty']->assign('page_url',   $url_info['page_url']);
			$GLOBALS['smarty']->assign('total',   $url_info['page_info']['total']);
			$GLOBALS['smarty']->assign('store_id',$store_id);
			$GLOBALS['smarty']->assign('productIDlist',$productIDlist);
			$GLOBALS['smarty']->assign('productlist',$rows);
			$GLOBALS['smarty']->display('store_link_product.htm');
		}
	
	/**************************************
    * 插入分类信息
	* @access  public
	* @param   array     $store  分类信息组
	*
	* @return  array
    **************************************/
	function SaveStore()
	{
		$store=$_REQUEST['store'];
		if(!empty($store))
		{
			$this->ControlStore->SaveStore($store);
		}
		$this->StoreList();
	}
	
	
	/**************************************
    * 分类信息详情
	* @access  public
	* @param   array     $store  分类信息组
	*
	* @return  array
    **************************************/
	function StoreDetail()
	{
		$store_id=intval($_REQUEST['store_id']);
		if($store_id)
		{
			$store=$this->ControlStore->StoreDetail($store_id);
		}
		$actionurl="./store.php?action=UpdateStore";
		$GLOBALS['smarty']->assign('actionurl',$actionurl);
		$GLOBALS['smarty']->assign('store',$store);
		$GLOBALS['smarty']->display('store_detail.htm');
	}
	
   /**************************************
    * 修改分类信息详情
	* @access  public
	* @param   array     $store  分类信息组
	*
	* @return  array
    **************************************/
	function UpdateStore()
	{
		if($_GET['type'] == 'product_sort')//判断是否修改产品在sort上显示顺序
		{   
			$store_id = intval($_GET['store_id']);
			$product_id = intval($_GET['product_id']);
			$store_product_sort  = intval($_GET['value']);
			if(!empty($store_id)&&!empty($product_id)&&!empty($store_product_sort))
			$this->ControlStore->UpdateProductSort($store_id,$product_id,$store_product_sort);
		}else
		{
			$store=$_REQUEST['store'];
			if(!empty($store))
			{
				$this->ControlStore->UpdateStore($store);
			}
			$this->StoreList();
		}
	}
	
	/**************************************
    * 删除分类信息详情
	* @access  public
	* @param   array     $store  分类信息组
	*
	* @return  array
    **************************************/
	function DeleteStore()
	{
		$store_id=intval($_REQUEST['store_id']);
		if($store_id)
		{
			$type = $this->ControlStore->DeleteStore($store_id);
			echo  $type;
		}
//		$this->StoreList();
	}
	
	/**************************************
    * 关联分类和产品
	* @access  public
	* @param   int     store_id    分类信息ID
	* @param   int     product_id  产品ID
	*
	* @return  array
    **************************************/
	function AddLinkProduct()
	{
		$product_id=intval($_REQUEST['product_id']);
		$store_id=intval($_REQUEST['store_id']);
		if($product_id && $store_id )
		{
			$this->ControlStore->AddLinkProduct($product_id,$store_id);
		}
	}
	
	
   /**************************************
    * 解除分类和产品
	* @access  public
	* @param   int     store_id    分类信息ID
	* @param   int     product_id  产品ID
	*
	* @return  array
    **************************************/
	function DelLinkProduct()
	{
		$product_id=intval($_REQUEST['product_id']);
		$store_id=intval($_REQUEST['store_id']);
		if($product_id && $store_id )
		{
			$this->ControlStore->DelLinkProduct($product_id,$store_id);
		}
	}
	
   /**************************************
    *  按条件搜索分类信息详情
	* @access  public
	* @param   int     $search_type  搜素类别
	* @param   string  $search_key   搜素关键字
	*
	* @return  array
    **************************************/
	function SearchStore()
	{
		$search_type=intval($_REQUEST['search_type']);
		$search_key=trim($_REQUEST['search_key']);
		if (empty($search_key))
		{
			$this->StoreList();
		}
		else 
		{
			$storelist=$this->ControlStore->SearchStore($search_type,$search_key);
			$GLOBALS['smarty']->assign('storelist',$storelist);
			$GLOBALS['smarty']->display('store_index.htm');
		}
	}
	
   /**************************************
    *  推荐分类信息
	* @access  public
	* @param   int     $store_id     分类的ID
	* @param   int     $rec_value    推荐值
	*
	* @return  array
    **************************************/
	function RecommendStore()
	{
		$store_id=intval($_REQUEST['store_id']);
		$rec_value=intval($_REQUEST['recvalue']);
		$this->ControlStore->RecommendStore($store_id,$rec_value);
		$this->StoreList();
	}
	
   /**************************************
    *  添加产品到分类中
	* @access  public
	* @param   int     $store_id     分类的ID
	*
	* @return  array
    **************************************/
	function ManageProduct($sto_id="")
	{
		if($sto_id)
		{
			$store_id=intval($sto_id); 
		}
		else 
		{
			$store_id=intval($_REQUEST['store_id']);
		}	
		$page = 1;
		      if (!empty($_REQUEST['page'])) {
			  $page = intval($_REQUEST['page']);
		      }
		$url_info=$this->ControlStore->getManageProductListPage($page,$store_id);
		$rows=$this->ControlStore->getManageProductList($url_info['page_info']['start'],15,$store_id);
		if($rows)
			{
				foreach ($rows as $key => $value)
				{
					if($value['store_product_state']==0)
					{
						$rows[$key]['store_product_state']='未发布';
					}
					else 
					{
						$rows[$key]['store_product_state']='发布';
					}
					if($value['store_product_is_recommend']==0)
					{
						$rows[$key]['store_product_is_recommend']='未推荐';
					}
					else 
					{
						$rows[$key]['store_product_is_recommend']='推荐';
					}
					if($value['store_product_verify']==0)
					{
						$rows[$key]['store_product_verify']='待审核';
					}
					elseif($value['store_product_verify']==1)
					{
						$rows[$key]['store_product_verify']='通过推荐';
					}
					elseif($value['store_product_verify']==2)
					{
						$rows[$key]['store_product_verify']='未通过推荐';
					}
					
					$rows[$key]['store_product_add_time']=date('Y-m-d',$value['store_product_add_time']);
				}
			}
		
		$GLOBALS['smarty']->assign('page_url',   $url_info['page_url']);
		$GLOBALS['smarty']->assign('total',   $url_info['page_info']['total']);
		$GLOBALS['smarty']->assign('store_id',$store_id);
		$GLOBALS['smarty']->assign('productlist',$rows);
		$GLOBALS['smarty']->display('store_manage_product.htm');
	}
	
   /**************************************
    *  从分类中删除产品
	* @access  public
	* @param   int     $store_id     分类的ID
	* @param   int     $product_id   产品的ID
	*
	* @return  array
    **************************************/
	function DelProduct($product_id,$store_id)
	{
		$product_id=intval($_REQUEST['product_id']);
		$store_id=intval($_REQUEST['store_id']);
		if($product_id && $store_id )
		{
			$this->ControlStore->DelLinkProduct($product_id,$store_id);
		}
		$this->ManageProduct($store_id);
	}
	
   /**************************************
    * 在分类的管理产品中发布产品
	* @access  public
	* @param   int     $store_id     分类的ID
	* @param   int     $state_value  发布状态的值
	* @param   int     $product_id   产品的ID
	*
	* @return  array
    **************************************/
	function StateProduct()
	{
		$product_id=intval($_REQUEST['product_id']);
		$state_value=intval($_REQUEST['state_value']);
		$store_id=intval($_REQUEST['store_id']);
		if($product_id && $store_id)
		{
			$this->ControlStore->StateProduct($product_id,$state_value,$store_id);
		}
		$this->ManageProduct($store_id);
	}
	
	/**************************************
    * 在分类的管理产品中推荐产品
	* @access  public
	* @param   int     $store_id     分类的ID
	* @param   int     $product_id   产品的ID
	* @param   int     $rec_value    推荐状态的值
	*
	* @return  array
    **************************************/
	function RecommendProduct()
	{
		$product_id=intval($_REQUEST['product_id']);
		$rec_value=intval($_REQUEST['rec_value']);
		$store_id=intval($_REQUEST['store_id']);
		if($product_id && $store_id)
		{
			$this->ControlStore->RecommendProduct($product_id,$rec_value,$store_id);
		}
		$this->ManageProduct($store_id);
	}
	
	/**************************************
    * 在分类的管理产品中审核产品
	* @access  public
	* @param   int     $store_id     分类的ID
	* @param   int     $product_id   产品的ID
	* @param   int     $verify       审核状态的值
	*
	* @return  array
    **************************************/
	function VerifyProduct()
	{
		$product_id=intval($_REQUEST['product_id']);
		$verify=intval($_REQUEST['verify']);
		$store_id=intval($_REQUEST['store_id']);
		if($product_id && $store_id)
		{
			$this->ControlStore->VerifyProduct($product_id,$verify,$store_id);
		}
		$this->ManageProduct($store_id);
	}
	
	/**************************************
    * 在分类的管理产品中按条件搜索产品
	* @access  public
	* @param   int     $store_id                     分类的ID
	* @param   int     $store_product_is_recommend   推荐状态值
	* @param   int     $store_product_state          发布状态的值
	* @param   int     $store_product_verify         审核状态的值
	* @param   int     $search_type                  关键字状态的值
	* @param   string  $search_key                   关键字的值
	* @param   int     $page                         分页的值
	*
	* @return  array
    **************************************/
	function ManageSearchProductList()
	{
		$store_id=intval($_REQUEST['store_id']);
		$store_product_is_recommend=intval($_REQUEST['store_product_is_recommend']);
		$store_product_state=intval($_REQUEST['store_product_state']);
		$store_product_verify=intval($_REQUEST['store_product_verify']);
		$search_type=intval($_REQUEST['search_type']);
		$search_key=trim($_REQUEST['$search_key']);
		$page = 1;
		      if (!empty($_REQUEST['page'])) {
			  $page = intval($_REQUEST['page']);
		      }
		$url_info=$this->ControlStore->getManageProductListPage($page,$store_id,$store_product_is_recommend,$store_product_state,$store_product_verify,$search_type,$search_key);
		$rows=$this->ControlStore->getManageProductList($url_info['page_info']['start'],15,$store_id,$store_product_is_recommend,$store_product_state,$store_product_verify,$search_type,$search_key);
		if($rows)
			{
				foreach ($rows as $key => $value)
				{
					if($value['store_product_state']==0)
					{
						$rows[$key]['store_product_state']='未发布';
					}
					else 
					{
						$rows[$key]['store_product_state']='发布';
					}
					if($value['store_product_is_recommend']==0)
					{
						$rows[$key]['store_product_is_recommend']='未推荐';
					}
					else 
					{
						$rows[$key]['store_product_is_recommend']='推荐';
					}
					if($value['store_product_verify']==0)
					{
						$rows[$key]['store_product_verify']='待审核';
					}
					elseif($value['store_product_verify']==1)
					{
						$rows[$key]['store_product_verify']='通过推荐';
					}
					elseif($value['store_product_verify']==2)
					{
						$rows[$key]['store_product_verify']='未通过推荐';
					}
					
					$rows[$key]['store_product_add_time']=date('Y-m-d',$value['product_add_time']);
				}
			}
		
		$GLOBALS['smarty']->assign('page_url',   $url_info['page_url']);
		$GLOBALS['smarty']->assign('total',   $url_info['page_info']['total']);
		$GLOBALS['smarty']->assign('store_id',$store_id);
		$GLOBALS['smarty']->assign('productlist',$rows);
		$GLOBALS['smarty']->display('store_manage_product.htm');
	}

}
	
	
$action    = $_REQUEST['action'];
$Store   = new Store;

if(method_exists($Store, $action)) {
	$Store->$action();
}else{
	$Store->index();
}
?>